package app.ep1.mapaUsp;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
import app.ep1.R;
import app.ep1.portoes.Road;
import app.ep1.portoes.RoadProvider;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapaUspActivity extends MapActivity {

	private final int MAX8022 = 8;
	private final int MAX8012 = 8;

	private MapaItensOverlay mapaPortoesOverlay;
	private MapaItensOverlay mapaInstitutosOverlay;
	private MapaItensOverlay mapaMuseusOverlay;
	private MapaItensOverlay mapaBibliotecasOverlay;
	private MapaItensOverlay mapaRestaurantesOverlay;
	private MapaItensOverlay mapaEsportesOverlay;
	private MapaItensOverlay mapaPracasOverlay;
	private MapaItensOverlay mapaHospitalOverlay;
	private MapaItensOverlay mapaArtesOverlay;
	private MapaItensOverlay mapaDancaOverlay;
	private MapaItensOverlay mapaCinemaOverlay;
	private MapaItensOverlay mapaCruspOverlay;
	private MapaItensOverlay mapaPrefeituraOverlay;
	private MapaItensOverlay mapaOutrosOverlay;
	private MapaItensOverlay mapaUsuarioOverlay;
	private List<Overlay> mapOverlays;
	private OverlayItem itemUsuario = null;
	private MapView mapView;

	private LocationManager locationManager;
	private LocationListener locationListener;

	LinearLayout linearLayout;
	private Road mRoad8022[];
	private Road mRoad8012[];
	private GeoPoint bus8022[];
	private GeoPoint bus8012[];

	public MapaItensOverlay getMapaPortoesOverlay() {
		return mapaPortoesOverlay;
	}

	public MapaItensOverlay getMapaInstitutosOverlay() {
		return mapaInstitutosOverlay;
	}

	public MapaItensOverlay getMapaRestaurantesOverlay() {
		return mapaRestaurantesOverlay;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapa_usp);

		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		// colocando o centro no IME
		GeoPoint geoPoint = new GeoPoint(converteParaE6(-23, 33, 33.61),
				converteParaE6(-46, 43, 54.47));
		mapView.getController().setCenter(geoPoint);
		mapView.getController().setZoom(15);

		mapOverlays = mapView.getOverlays();

		Drawable drawableGate = this.getResources()
				.getDrawable(R.drawable.gate);
		mapaPortoesOverlay = new MapaItensOverlay(drawableGate, this);

		Drawable drawableInstitutos = this.getResources().getDrawable(
				R.drawable.graduation);
		mapaInstitutosOverlay = new MapaItensOverlay(drawableInstitutos, this);

		Drawable drawableMuseum = this.getResources().getDrawable(
				R.drawable.museum);
		mapaMuseusOverlay = new MapaItensOverlay(drawableMuseum, this);

		Drawable drawableBook = this.getResources()
				.getDrawable(R.drawable.book);
		mapaBibliotecasOverlay = new MapaItensOverlay(drawableBook, this);

		Drawable drawableRestaurant = this.getResources().getDrawable(
				R.drawable.restaurant);
		mapaRestaurantesOverlay = new MapaItensOverlay(drawableRestaurant, this);

		Drawable drawableSport = this.getResources().getDrawable(
				R.drawable.sport);
		mapaEsportesOverlay = new MapaItensOverlay(drawableSport, this);

		Drawable drawableTree = this.getResources()
				.getDrawable(R.drawable.tree);
		mapaPracasOverlay = new MapaItensOverlay(drawableTree, this);

		Drawable drawableHospital = this.getResources().getDrawable(
				R.drawable.hospital);
		mapaHospitalOverlay = new MapaItensOverlay(drawableHospital, this);

		Drawable drawableArt = this.getResources().getDrawable(R.drawable.art);
		mapaArtesOverlay = new MapaItensOverlay(drawableArt, this);

		Drawable drawableDance = this.getResources().getDrawable(
				R.drawable.dance);
		mapaDancaOverlay = new MapaItensOverlay(drawableDance, this);

		Drawable drawableMovies = this.getResources().getDrawable(
				R.drawable.movies);
		mapaCinemaOverlay = new MapaItensOverlay(drawableMovies, this);

		Drawable drawableCrusp = this.getResources().getDrawable(
				R.drawable.crusp);
		mapaCruspOverlay = new MapaItensOverlay(drawableCrusp, this);

		Drawable drawablePrefeitura = this.getResources().getDrawable(
				R.drawable.prefeitura);
		mapaPrefeituraOverlay = new MapaItensOverlay(drawablePrefeitura, this);

		Drawable drawableBuilding = this.getResources().getDrawable(
				R.drawable.building);
		mapaOutrosOverlay = new MapaItensOverlay(drawableBuilding, this);

		Drawable drawableUsuario = this.getResources().getDrawable(
				R.drawable.ic_launcher);
		mapaUsuarioOverlay = new MapaItensOverlay(drawableUsuario, this);

		MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,
				mapView);
		myLocationOverlay.enableMyLocation();

		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationListener = new MyLocationOverlay(this, mapView) {
			@Override
			public synchronized void onLocationChanged(Location location) {
				super.onLocationChanged(location);

				if (location == null)
					return;

				GeoPoint geoPoint = new GeoPoint(
						(int) (location.getLatitude() * 1000000),
						(int) (location.getLongitude() * 1000000));

				mapOverlays.remove(itemUsuario);

				mapaUsuarioOverlay.removeOverlayItem(itemUsuario);

				if (isDentroDaUsp(geoPoint)) {
					itemUsuario = new OverlayItem(geoPoint, "Você",
							"Está aqui!");

					mapaUsuarioOverlay.insereOverlayItem(itemUsuario);
					mapOverlays.add(mapaUsuarioOverlay);

					mapView.invalidate();
				}
			}
		};

		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, locationListener);

		criaOverlayDeCircular();

		criaOverlaysDeInstitutos();

		criaOverlayDoUsuario();

	}

	private void criaOverlayDeCircular() {
		bus8022 = new GeoPoint[MAX8022];
		bus8022[0] = new GeoPoint(-23571600, -46708700);// Metro Butanta
		bus8022[1] = new GeoPoint(-23562600, -46712400);// Mello Moraes (perto
														// da portaria)
		bus8022[2] = new GeoPoint(-23552900, -46727400);// Mello Moraes (fim)
		bus8022[3] = new GeoPoint(-23565200, -46725600);// Lineu Prestes
														// (Quimica)
		bus8022[4] = new GeoPoint(-23568000, -46740300);// Perto P3
		bus8022[5] = new GeoPoint(-23559200, -46741000);// Almeida Prado
		bus8022[6] = new GeoPoint(-23564700, -46728800);// Rua do Matao
		bus8022[7] = new GeoPoint(-23571600, -46708700);// Metro Butanta

		bus8012 = new GeoPoint[MAX8012];
		bus8012[0] = new GeoPoint(-23571600, -46708700);// Metro Butanta
		bus8012[1] = new GeoPoint(-23559400, -46740200);// Almeida Prado
		bus8012[2] = new GeoPoint(-23568000, -46740300);// Perto P3
		bus8012[3] = new GeoPoint(-23564700, -46728800);// Rua do Matao
		bus8012[4] = new GeoPoint(-23554100, -46726100);// Mello Moraes (fim)
		bus8012[5] = new GeoPoint(-23558200, -46727100);// ECA
		bus8012[6] = new GeoPoint(-23557400, -46720000);// Mello Moraes(meio)
		bus8012[7] = new GeoPoint(-23571600, -46708700);// Metro Butanta

		drawRoute();

	}

	void drawRoute() {
		new Thread() {
			@Override
			public void run() {
				double fromLat, fromLon, toLat, toLon;
				String url;
				InputStream is;
				mRoad8012 = new Road[MAX8012 - 1];
				mRoad8022 = new Road[MAX8022 - 1];
				// for(int i = 0; i < 1; i++) {
				for (int i = 0; i < MAX8022 - 1; i++) {
					fromLat = (double) (bus8022[i].getLatitudeE6()) / 1000000;
					fromLon = (double) (bus8022[i].getLongitudeE6()) / 1000000;
					toLat = (double) (bus8022[i + 1].getLatitudeE6()) / 1000000;
					toLon = (double) (bus8022[i + 1].getLongitudeE6()) / 1000000;
					url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
					is = getConnection(url);
					mRoad8022[i] = RoadProvider.getRoute(is);
				}

				for (int i = 0; i < MAX8012 - 1; i++) {
					fromLat = (double) (bus8012[i].getLatitudeE6()) / 1000000;
					fromLon = (double) (bus8012[i].getLongitudeE6()) / 1000000;
					toLat = (double) (bus8012[i + 1].getLatitudeE6()) / 1000000;
					toLon = (double) (bus8012[i + 1].getLongitudeE6()) / 1000000;
					url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
					is = getConnection(url);
					mRoad8012[i] = RoadProvider.getRoute(is);
				}

				mHandler.sendEmptyMessage(0);
			}
		}.start();
	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			for (int i = 0; i < MAX8012 - 1; i++) {
				MapOverlay mapOverlay1 = new MapOverlay(mRoad8012[i], mapView,
						Color.RED);
				mapOverlays.add(mapOverlay1);
			}

			for (int i = 0; i < MAX8022 - 1; i++) {
				MapOverlay mapOverlay2 = new MapOverlay(mRoad8022[i], mapView,
						Color.BLUE);
				mapOverlays.add(mapOverlay2);
			}

			mapView.invalidate();
		};
	};

	private InputStream getConnection(String url) {
		InputStream is = null;
		try {
			URLConnection conn = new URL(url).openConnection();
			is = conn.getInputStream();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return is;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	public void criaOverlayDoUsuario() {
		Location location = locationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);

		if (location == null)
			return;

		GeoPoint geoPoint = new GeoPoint(
				(int) (location.getLatitude() * 1000000),
				(int) (location.getLongitude() * 1000000));

		if (isDentroDaUsp(geoPoint)) {
			itemUsuario = new OverlayItem(geoPoint, "Você", "Está aqui!");

			mapaUsuarioOverlay.insereOverlayItem(itemUsuario);
			mapOverlays.add(mapaUsuarioOverlay);
		}
	}

	private boolean isDentroDaUsp(GeoPoint geoPoint) {
		int latitude = geoPoint.getLatitudeE6();
		int longitude = geoPoint.getLongitudeE6();

		if (latitude <= -23550000 && latitude >= -23574000
				&& longitude <= -46710000 && longitude >= -46748000)
			return true;

		return false;
	}

	public void criaOverlaysDeInstitutos() {

		OverlayItem overlayItem;

		// Portões
		overlayItem = new OverlayItem(new GeoPoint(-23565590, -46711840), "P1",
				"Portão 1");
		mapaPortoesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23550615, -46732455), "P2",
				"Portão 2");
		mapaPortoesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23569240, -46741390), "P3",
				"Portão 3");
		mapaPortoesOverlay.insereOverlayItem(overlayItem);

		// Institutos
		overlayItem = new OverlayItem(new GeoPoint(-23558400, -46731900),
				"IME", "Instituto de Matemática e Estatística");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23565600, -46725500), "IQ",
				"Instituto de Química");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23567645, -46738921), "FO",
				"Faculdade de Odontologia");// ok
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23566642, -46741487), "FM",
				"Faculdade de Medicina");// ok
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23566977, -46741058),
				"FOFITO", "Fonoaudiologia, Fisioterapia e Terapia Ocupacional");// ok
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23569700, -46739700),
				"FMVZ", "Faculdade de Medicina Veterinária e Zootecnia");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23563420, -46713360),
				"EEFE", "Escola de Educação Física e Esportes");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23560344, -46735038), "IF",
				"Instituto de Física");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-2355799, -46727000), "ECA",
				"Escola de Comunicação e Artes");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23555810, -46725030), "IP",
				"Instituto de Psicologia");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23556739, -46732766),
				"POLI", "Escola Politécnica de São Paulo");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23559070, -46729263),
				"FEA", "Faculdade de Economia e Administração");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23560857, -46732023), "IO",
				"Instituto Oceonográfico");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23567606, -46732260),
				"ICB", "Instituto de Ciências Biomédicas");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23565954, -46730222), "IB",
				"Instituto de Biociências");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23561745, -46727389), "IG",
				"Instituto de Geociências");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23563653, -46725179),
				"FCF", "Faculdade de Ciências Farmacêuticas");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23562610, -46724821),
				"FFLCH", "Faculdade de Filosofia, Letras e Ciências Humanas");// ok
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23559530, -46734400),
				"IAG", "Instituto de Astronomia e Geofísica");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23566650, -46728130),
				"FAU", "Faculdade de Arquitetura e Urbanismo");
		mapaInstitutosOverlay.insereOverlayItem(overlayItem);

		// Museus
		overlayItem = new OverlayItem(new GeoPoint(-23559766, -46741180),
				"MAE", "Museu de Arqueologia e Etnologia");
		mapaMuseusOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23559000, -46722100),
				"MAC", "Museu de Arte Contemporânea");
		mapaMuseusOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23564245, -46740303), "MA",
				"Museu de Anatomia");
		mapaMuseusOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23560580, -46731661), "MO",
				"Museu Oceanográfico");
		mapaMuseusOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23561369, -46728606), "MG",
				"Museu de Geociências");
		mapaMuseusOverlay.insereOverlayItem(overlayItem);

		// Bibliotecas
		overlayItem = new OverlayItem(new GeoPoint(-23558251, -46727027),
				"ECA", "Biblioteca da ECA");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23562679, -46725889), "",
				"Biblioteca Florestan Fernnades");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23560722, -46734880),
				"Física", "Biblioteca do Instituto de Física ");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23559000, -46731800), "",
				"Biblioteca do Mario de Andrade ");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23556100, -46730200),
				"Engenharia Elétrica",
				"Biblioteca do Instituto de Engenharia Elétrica ");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23555000, -46725400), "",
				" Biblioteca Dante Moreira Leite  ");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23561100, -46722100), "",
				"Biblioteca Brasiliana ");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23569100, -46740700), "",
				"Biblioteca Virginie Buff D'Ápice ");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		// Editora
		overlayItem = new OverlayItem(new GeoPoint(-23562116, -46722555),
				"EDUSP", "Editora da Universidade de São Paulo");
		mapaBibliotecasOverlay.insereOverlayItem(overlayItem);

		// Restaurantes
		overlayItem = new OverlayItem(new GeoPoint(-23552608, -46731951), "",
				"Toca do Urso Restaurante");
		mapaRestaurantesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23558400, -46735600), "",
				"Restaurante da Física");
		mapaRestaurantesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23557400, -46738200), "",
				"Bandejão COCESP");
		mapaRestaurantesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23560290, -46721470),
				"BACEN", "Bandejão Central");
		mapaRestaurantesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23562300, -46725700), "",
				"Bandejão da Química");
		mapaRestaurantesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23560000, -46734800), "",
				"Bandejão da Física");
		mapaRestaurantesOverlay.insereOverlayItem(overlayItem);

		// Esportes
		overlayItem = new OverlayItem(new GeoPoint(-23560050, -46716670),
				"CEPEUSP", "Centro de Práticas Esportivas");
		mapaEsportesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23556150, -46721400), "",
				"Raia Olímpica");
		mapaEsportesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23558600, -46731700), "",
				"Clube dos Professores");
		mapaEsportesOverlay.insereOverlayItem(overlayItem);

		// Praças
		overlayItem = new OverlayItem(new GeoPoint(-23559550, -46724240), "",
				"Praça do Relógio");
		mapaPracasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23559874, -46726484), "",
				"Praça dos Bancos");
		mapaPracasOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23560860, -46720730), "",
				"Praça da Reitoria");
		mapaPracasOverlay.insereOverlayItem(overlayItem);

		// Hospital
		overlayItem = new OverlayItem(new GeoPoint(-23565440, -46740070), "",
				"Hospital Universitário");
		mapaHospitalOverlay.insereOverlayItem(overlayItem);

		// Prefeitura
		overlayItem = new OverlayItem(new GeoPoint(-23566918, -46725967), "",
				"Fundação Prefeito Faria Lima");
		mapaPrefeituraOverlay.insereOverlayItem(overlayItem);

		// Reitoria
		overlayItem = new OverlayItem(new GeoPoint(-23561240, -46722160), "",
				"Reitoria");
		mapaPrefeituraOverlay.insereOverlayItem(overlayItem);

		// Conjunto Residencial
		overlayItem = new OverlayItem(new GeoPoint(-23559560, -46721750),
				"CRUSP", "Conjunto Residencial da USP");
		mapaCruspOverlay.insereOverlayItem(overlayItem);

		// Cinema
		overlayItem = new OverlayItem(new GeoPoint(-23558520, -46721390), "",
				"CINUSP _ Paulo Emílio");
		mapaCinemaOverlay.insereOverlayItem(overlayItem);

		// Dança
		overlayItem = new OverlayItem(new GeoPoint(-23561816, -46733560), "",
				"Dança de Salão");
		mapaDancaOverlay.insereOverlayItem(overlayItem);

		// Artes
		overlayItem = new OverlayItem(new GeoPoint(-23564210, -46715270), "",
				"Paço das Artes");
		mapaArtesOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23563240, -46720150), "",
				"Casa da Cultura Japonesa");
		mapaArtesOverlay.insereOverlayItem(overlayItem);

		// Fundação
		overlayItem = new OverlayItem(new GeoPoint(-23565390, -46712970), "",
				"Fundação de Apoio a Universidade de São Paulo");
		mapaOutrosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23554200, -46724700),
				"FUVEST", "Fundação Universitária para o Vestibular");
		mapaOutrosOverlay.insereOverlayItem(overlayItem);

		// Sociedades
		overlayItem = new OverlayItem(new GeoPoint(-23562151, -46723365), "",
				"Sociedade Brasileira de Geologia");
		mapaOutrosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23568122, -46732302), "",
				"Sociedade Brasileira de Microbiologia");
		mapaOutrosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23560727, -46732610),
				"Física", "Sociedade Brasileira de Física");
		mapaOutrosOverlay.insereOverlayItem(overlayItem);

		overlayItem = new OverlayItem(new GeoPoint(-23566502, -46738181), "",
				"Comissão Nacional de Energia Nuclear");
		mapaOutrosOverlay.insereOverlayItem(overlayItem);

		// Associação
		overlayItem = new OverlayItem(new GeoPoint(-23563520, -46712350), "",
				"Associação Brasileira de Distrofia Muscular");
		mapaOutrosOverlay.insereOverlayItem(overlayItem);

		/* no fim */
		mapOverlays.add(mapaPortoesOverlay);
		mapOverlays.add(mapaInstitutosOverlay);
		mapOverlays.add(mapaMuseusOverlay);
		mapOverlays.add(mapaBibliotecasOverlay);
		mapOverlays.add(mapaRestaurantesOverlay);
		mapOverlays.add(mapaEsportesOverlay);
		mapOverlays.add(mapaPracasOverlay);
		mapOverlays.add(mapaHospitalOverlay);
		mapOverlays.add(mapaPrefeituraOverlay);
		mapOverlays.add(mapaCruspOverlay);
		mapOverlays.add(mapaCinemaOverlay);
		mapOverlays.add(mapaDancaOverlay);
		mapOverlays.add(mapaArtesOverlay);
		mapOverlays.add(mapaOutrosOverlay);
	}

	private int converteParaE6(int graus, int minutos, double segundos) {
		double somaMinSeg = 60 * (double) minutos + segundos;
		somaMinSeg = somaMinSeg / 3600;

		double somaFinal;
		if (graus >= 0)
			somaFinal = (double) graus + somaMinSeg;
		else
			somaFinal = (double) graus - somaMinSeg;

		return (int) (somaFinal * 1000000);
	}

	class MapOverlay extends com.google.android.maps.Overlay {
		Road mRoad;
		ArrayList<GeoPoint> mPoints;
		int mColor;

		public MapOverlay(Road road, MapView mv, int color) {
			mRoad = road;
			mColor = color;
			if (road.mRoute.length > 0) {
				mPoints = new ArrayList<GeoPoint>();
				for (int i = 0; i < road.mRoute.length; i++) {
					mPoints.add(new GeoPoint(
							(int) (road.mRoute[i][1] * 1000000),
							(int) (road.mRoute[i][0] * 1000000)));
				}
			}
		}

		double dist(GeoPoint p, GeoPoint q) {
			return (p.getLatitudeE6() - q.getLatitudeE6())
					* (p.getLatitudeE6() - q.getLatitudeE6())
					+ (p.getLongitudeE6() - q.getLongitudeE6())
					* (p.getLongitudeE6() - q.getLongitudeE6());
		}

		@Override
		public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
			super.draw(canvas, mv, shadow);
			drawPath(mv, canvas);
			return true;
		}

		public void drawPath(MapView mv, Canvas canvas) {
			int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
			Paint paint = new Paint();
			paint.setColor(mColor);
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(3);
			for (int i = 0; i < mPoints.size(); i++) {
				Point point = new Point();
				mv.getProjection().toPixels(mPoints.get(i), point);
				x2 = point.x;
				y2 = point.y;
				if (i > 0) {
					canvas.drawLine(x1, y1, x2, y2, paint);
				}
				x1 = x2;
				y1 = y2;
			}
		}
	}
}
